package logicals

import (
	"easyblog/models"
	"easyblog/utils/db"
	"easyblog/utils/errmsg"
	"gorm.io/gorm"
)

type CategoryLogical struct {}

var DefaultCategory = CategoryLogical{}

/**
* CreateCategory
* @Description: 创建分类
* @receiver this
* @param category
* @return int
*/
func (this *CategoryLogical) CreateCategory(category *models.Category) int {
	err := db.Db.Create(category).Error
	if err != nil {
		return errmsg.ERROR
	}
	return errmsg.SUCCSE
}

/**
* CheckCateExists
* @Description: 检查分类是否存在
* @receiver this
* @param categoryName
* @return int
*/
func (this *CategoryLogical) CheckCategoryExists(categoryName string)int {
	var category models.Category
	db.Db.Where("name = ?", categoryName).First(&category)
	if category.Name != "" {
		return errmsg.ERROR_CATEGORYNAME_USED //3001
	}
	return errmsg.SUCCSE //200
}

/**
* EditCategory
* @Description: 编辑分类
* @receiver this
* @param categoryId
* @param category
* @return int
*/
func (this *CategoryLogical) EditCategory(categoryId int, category *models.Category) int {
	var maps = make(map[string]interface{})
	maps["name"] = category.Name

	err := db.Db.Model(&models.Category{}).Where("id = ?", categoryId).Updates(maps).Error
	if err != nil {
		return errmsg.ERROR
	}
	return errmsg.SUCCSE
}

/**
* GetOneCategory
* @Description: 通过id获取分类
* @receiver this
* @param categoryId
* @return models.Category
* @return int
*/
func (this *CategoryLogical) GetCategoryById(categoryId int) (models.Category,int) {
	var category models.Category
	err := db.Db.Where("id = ?", categoryId).First(&category).Error
	if err != nil {
		return category,errmsg.ERROR
	}
	return category,errmsg.SUCCSE
}

/**
* DeleteCategory
* @Description: 删除分类
* @receiver this
* @param categoryId
* @return int
*/
func (this *CategoryLogical) DeleteCategory(categoryId int) int {
	err := db.Db.Where("id = ?", categoryId).Delete(&models.Category{}).Error
	if err != nil {
		return errmsg.ERROR
	}
	return errmsg.SUCCSE
}

/**
* GetPageCategories(pageSize int, pageNum int)
* @Description: 分页查询分类列表
* @receiver this
* @param pageSize
* @param pageNum
* @return []models.Category
* @return int64
*/
func (this *CategoryLogical)  GetPageCategories(pageSize int, pageNum int) ([]models.Category, int64){
	var cates []models.Category
	var total int64

	err := db.Db.Find(&cates).Limit(pageSize).Offset(pageSize * (pageNum - 1)).Error
	db.Db.Model(&cates).Count(&total)
	// 不存在
	if err != nil && gorm.ErrRecordNotFound != nil {
		return nil, 0
	}
	return cates, total
}

/**
* GetCategorys
* @Description: 获取前20个category
* @receiver this
* @return interface{}
* @return interface{}
*/
func (this *CategoryLogical) GetHotCategorys() ([]models.Category, int) {
	var cates []models.Category
	err := db.Db.Find(&cates).Limit(20).Error
	if err != nil{
		return nil, errmsg.ERROR
	}
	return cates,errmsg.SUCCSE
}

/**
* updateCateNum
* @Description: 更新分类下文章的数量
* @param cateId
* @param action
*/
func updateCateNum(cateId int, action string){
	err := db.Db.Model(&models.Category{}).Where("id = ?", cateId).UpdateColumn("num", gorm.Expr("num " + action + " 1")).Error
	if err != nil{
		return
	}
	return
}